<?php

namespace Biz\Play;

use Base\Exception\NoRecordException;

class Active extends \Base\BaseBiz
{
    const STATUS = 0;
    const STATUS_YES = 1;
    const STATUS_NO = 2;

    const STATUS_ARR = array(
        self::STATUS => '等待中',
        self::STATUS_YES => '竞猜获胜',
        self::STATUS_NO => '竞猜失败'
    );

    //添加竞猜
    public static function insert($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $active_orm = new \Orm\PlayActive\PlayActiveClass();
            $active_orm['uid'] = $_orm['uid']??'';
            $active_orm['play_show_id'] = $_orm['play_show_id']??'';
            $active_orm['play_target_id'] = $_orm['play_target_id']??'';
            $active_orm['play_type'] = $_orm['play_type']??'';
            $active_orm['multiple'] = $_orm['multiple']??1;
            $active_orm['put_date'] = $_orm['put_date']??date('Y-m-d');
            $active_orm['status'] = $_orm['status']??0;
            if(!$active_orm['play_show_id'] || !$active_orm['play_target_id'] || !$active_orm['play_type']){
                throw new \Exception('数据错误，请求失败');
            }
            $active_orm->insert();
            if($active_orm['id']){
                $rs['isok'] = true;
            }else{
                throw new \Exception('新增数据失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    //更新信息
    public static function update($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $active_orm = new \Orm\PlayActive\PlayActiveClass();
            if(!isset($_orm['id']) || !$_orm['id']){
                throw new \Exception('数据错误');
            }
            $active_orm['id'] = $_orm['id'];
            $active_orm['uid'] = $_orm['uid']??'';
            $active_orm['play_show_id'] = $_orm['play_show_id']??'';
            $active_orm['play_type'] = $_orm['play_type']??1;
            $active_orm['play_target_id'] = $_orm['play_target_id']??'';
            $active_orm['multiple'] = $_orm['multiple']??1;
            $active_orm['put_date'] = $_orm['put_date']??date('Y-m-d');
            $active_orm['status'] = $_orm['status']??0;

            $is_update = $active_orm->update();
            if($is_update){
                $rs['isok'] = true;
            }else{
                throw new \Exception('更新失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    /**
     * 获取信息
     */
    public static function getInfo($id)
    {
        $active_orm = new \Orm\PlayActive\PlayActiveClass();
        $active_orm->loadById($id);

        return $active_orm;
    }

    /*
     * 获取列表
     */
    public static function getList($where, $now = 1, $size = 0)
    {
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $active_list = new \Orm\PlayActive\PlayActiveList();
        if (!empty($where)) {
            $active_list['where'] = $where;
        } else {
            $active_list['where'] = '';
        }
        $active_list['limit'] = $size;
        $active_list['offset'] = ($now - 1) * $size;
        $active_list['order'] = '`id` DESC';
        $active_list['key_by'] = 'id';
        $active_list->load();

        $result = array();
        $result['lists'] = $active_list['lists'];
        $result['total'] = $active_list['total'];
        return $result;
    }

    public static function getListByShow($where, $now = 1, $size = 0){
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $active_model = new \Orm\PlayActive\PlayActiveModel();
        $offset = ($now-1) * $size;
        $result = $active_model->fetchAll('*','id DESC',$where,$size,$offset);
        return $result;
    }

    public function getListWithInfo($where, $now = 1, $size = 0)
    {
        //比赛队伍
        $player_biz = new \Biz\Player();
        $player_list = $player_biz->getPlayerList('', 1, 100);
        $player_list = $player_list['lists'];

        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $active_list = new \Orm\PlayActive\PlayActiveList();
        if (!empty($where)) {
            $active_list['where'] = $where;
        } else {
            $active_list['where'] = '';
        }
        $active_list['limit'] = $size;
        $active_list['offset'] = ($now - 1) * $size;
        $active_list['order'] = '`id` DESC';
        $active_list['key_by'] = 'id';
        $active_list->load();

        $return_list = array();
        if($active_list['lists']){
            foreach ($active_list['lists'] as $val){
                $showInfo = \Biz\Play\Showings::getInfo($val['play_show_id']);
                $targetInfo = \Biz\Play\Settings::getInfo($val['play_target_id']);
                try{
                    $userInfo = \Biz\User::getInfo($val['uid']);
                    $val['uname'] = $userInfo['nickname']??'';
                }catch (\Exception $e){
                    $val['uname'] = '';
                }

                $val['play_info'] = ($player_list[$showInfo['player_a']]['player_name']??'') .' vs '.($player_list[$showInfo['player_b']]['player_name']??'');
                $val['target_info'] = $targetInfo['title'];
                $val['play_type'] = \Biz\Play\Settings::getTypeStr($val['play_type']);
                $return_list[] = $val;
            }
        }

        $result = array();
        $result['lists'] = $active_list['lists'];
        $result['total'] = $active_list['total'];
        return $result;
    }

    public static function getTotal($where){
        $active_model = new \Orm\PlayActive\PlayActiveModel();
        $active_nums = $active_model->fetchRowCount('DISTINCT play_show_id',$where);
        return $active_nums['num']??0;
    }

    /*
     * 获取某一场次我的竞猜信息
     */

    public static function getResultByUidAndSid($uid,$sid)
    {
        $active_list = new \Orm\PlayActive\PlayActiveList();
        $active_list['where'] = 'uid = '.$uid.' and play_show_id='.$sid;
        $active_list['order'] = '`id` DESC';
        $active_list['key_by'] = 'id';
        $active_list->load();

        $result = array(\Biz\Play\Settings::TYPE_A => 0,\Biz\Play\Settings::TYPE_B => 0,\Biz\Play\Settings::TYPE_C => 0);
        if($active_list['lists']) {
            foreach ($active_list['lists'] as $item) {
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_A) {
                    $result[\Biz\Play\Settings::TYPE_A] = $item['play_target_id'];
                }
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_B) {
                    $result[\Biz\Play\Settings::TYPE_B] = $item['play_target_id'];
                }
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_C) {
                    $result[\Biz\Play\Settings::TYPE_C] = $item['play_target_id'];
                }
            }
        }
        return $result;
    }
}
